package com.ruoyi.web.zbAdmin.controller;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.web.zbAdmin.controller.param.UserViewCountQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author liandyao
 * @date 2022/12/14 16:55
 */
@Api(tags="才能招标-用户查看了多少数据管理")
@RestController
@RequestMapping("/zbApi/userView")
@Slf4j
public class UserViewCountController {

    @Autowired
    RedisCache redisCache;

    @Data
    class UserViewCount{
        private String userId ;
        private String type ;
        private Integer count ;
    }

    @ApiOperation("查看用户查看的次数")
    @GetMapping("/getUserViewCount")
    public AjaxResult getUserViewCount(UserViewCountQuery queryParams){
        Collection<String> keys =null;
        if(StrUtil.isNotBlank(queryParams.getUserId())){
            keys = redisCache.keys("ZHAOBIAO:*:view_count:"+queryParams.getUserId());
        }else {
            keys = redisCache.keys("ZHAOBIAO:*:view_count:*");
        }

        List<UserViewCount> list = new ArrayList<UserViewCount>();
        //模拟分页
        int pageSize = queryParams.getPageSize();
        int start = queryParams.getCurPage()*pageSize ;
        List<String> listKeys = new ArrayList<>(keys);
        for(int i=start,j=0;i<keys.size();i++,j++){
            //如果达到pageSize,则跳出循环
            if(j==pageSize){
                break ;
            }

            String key = listKeys.get(i);
            log.info("key是:"+key);
            UserViewCount vc = new UserViewCount();
            if(key.indexOf("company")>-1){
                vc.setType("查看公司");
            }else if(key.indexOf("detail")>-1){
                vc.setType("查看招标");
            }
            vc.setUserId(key);
            vc.setCount(redisCache.getCacheObject(key));
            list.add(vc);

        }


        return AjaxResult.success("成功",TableDataInfo.success(list,keys.size()));
    }

    @ApiOperation("用户次数清零")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "需要清空的key", dataType = "String", dataTypeClass = String.class,example = "ZHAOBIAO:detail:view_count:628282883000212"),
    })
    @PostMapping("/update/{key}")
    public AjaxResult update(@PathVariable  String key){
        log.info(key);
        Assert.notNull(key,"key不能为空");
        //Integer count = redisCache.getCacheObject(key);
        redisCache.setCacheObject(key,0);
        log.info("清零完成:"+key);
        return AjaxResult.success("清零完成");


    }
}
